package com.pospi.pai.yunpos.report;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.util.GetData;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import lib.kingja.switchbutton.SwitchMultiButton;

public class KhReportActivity extends Base2Activity {

    @Bind(R.id.switch_button)
    SwitchMultiButton switch_button;
    @Bind(R.id.tv_qkze)
    TextView tv_qkze;
    @Bind(R.id.tv_ysje)
    TextView tv_ysje;
    @Bind(R.id.tv_yinsje)
    TextView tv_yinsje;


    private List<KhBeen> beens = new ArrayList<>();

    private String qk;
    private String ys;
    private String yins;
    private MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_khwl);
        ButterKnife.bind(this);
        setTitle("客户往来统计");
        setMenuImage(R.drawable.icon_search_white);
        init();
        downStore();
        downSup();
        downCus();
    }
    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        Intent intent = new Intent(this, KHSearchActivity.class);
        startActivity(intent);

    }



    private void init() {
        switch_button.setText("今天","昨天","近三天","近七天","近一月");
        switch_button.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                switch (position) {
                    case 0:
                        startrq = GetData.getAnyTime(0);
                        endrq = GetData.getAnyTime(0);
                        searchBillfast();
                        break;
                    case 1:
                        startrq = GetData.getAnyTime(1);
                        endrq = GetData.getAnyTime(1);
                        searchBillfast();
                        break;
                    case 2:
                        startrq = GetData.getAnyTime(3);
                        endrq = GetData.getAnyTime(0);
                        searchBillfast();
                        break;
                    case 3:
                        startrq = GetData.getAnyTime(7);
                        endrq = GetData.getAnyTime(0);
                        searchBillfast();
                        break;
                    case 4:
                        startrq = GetData.getAnyTime(30);
                        endrq = GetData.getAnyTime(0);
                        searchBillfast();
                        break;
                }
            }
        });


        ListView bill_list = (ListView) findViewById(R.id.list_bill);
        adapter = new MyAdapter();
        bill_list.setAdapter(adapter);
        bill_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(KhReportActivity.this, KHBillListActivity.class);
                intent.putExtra("custid", beens.get(i).getCustid());
                intent.putExtra("person1", "");
                startActivity(intent);
            }
        });
        searchBillfast();

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        switch_button.clearSelection();
        startrq = intent.getStringExtra("startrq");
        endrq = intent.getStringExtra("endrq");
        market = intent.getStringExtra("market");
        custid = intent.getStringExtra("custid");
        person1 = intent.getStringExtra("person1");
        searchBillfast();

    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return beens.size();
        }

        @Override
        public Object getItem(int i) {
            return beens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder ;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(KhReportActivity.this).inflate(R.layout.activity_kh_item, null);
                holder.qk = view.findViewById(R.id.tv_qk);
                holder.name = view.findViewById(R.id.tv_name);
                holder.ys = view.findViewById(R.id.tv_ys);
                holder.yins = view.findViewById(R.id.tv_yins);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            KhBeen been = beens.get(i);
            holder.name.setText(been.getName()+been.getNum());
            holder.qk.setText(been.getQkje());
            holder.ys.setText(been.getYe());
            holder.yins.setText(been.getPayje());

            return view;
        }
        class Holder{
            TextView name;
            TextView qk;
            TextView ys;
            TextView yins;

        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                   closeLodindDialog();
                    tv_qkze.setText("欠款总额："+qk);
                    tv_ysje.setText("已收金额："+ys);
                    tv_yinsje.setText("应收金额："+yins);
                    adapter.notifyDataSetChanged();

                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
            }

        }
    };
    private void setbill(boolean o){
        if (o) {

        } else {

            beens.clear();
            adapter.notifyDataSetChanged();
        }

    }

    //下载门店
    private void downStore() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login",Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.STORE_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }
    //下载供应商
    private void downSup() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getsupplier");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.SUP_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }
    //下载客员工
    private void downCus() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getperson");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.CUS_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }
        });
    }


    private String startrq = GetData.getAnyTime(0);
    private String endrq = GetData.getAnyTime(0);
    private String market;
    private String custid;
    private String person1;
    private void searchBillfast() {
       showLodingDialog();
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "report.Mwltj");
        params.put("fun", "find_wlhzcust");

//        params.put("page", "");
//        params.put("limit", "");


        JSONObject object = new JSONObject();
        try {
            object.put("startrq", startrq);
            object.put("endrq", endrq);
            object.put("person1", person1);
            object.put("market", market);
            object.put("custid", custid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("login",params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("khre", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    Log.i("login", jsonObject.getString("errMsg"));
                    List<KhBeen> khBeens = new Gson().fromJson(result.toString(),
                            new TypeToken<List<KhBeen>>() {
                            }.getType());
                    beens.clear();
                    beens.addAll(khBeens);
                    JSONObject sumrow = jsonObject.getJSONObject("sumrow");
                    qk = sumrow.getString("zqkje");
                    ys = sumrow.getString("zpayje");
                    yins = sumrow.getString("zye");
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });

    }
}
